package ch18.v3.view;

import ch18.v3.controller.StudentManagerController;
import ch18.v3.model.Family;
import ch18.v3.model.Student;
import ch18.v3.util.SysInput;
import ch18.v3.util.SysOutput;

import java.util.ArrayList;
import java.util.List;

public class StudentMIS {

    static int no = 1;
    public StudentManagerController stuInfoMgrController;

    public StudentMIS() {
        SysOutput.show("欢迎使用学生信息管理系统系统\n请按提示操作");
        stuInfoMgrController = new StudentManagerController();
    }

    public void start() {
        SysOutput.show("1.添加学生信息\n2.删除学生信息\n3.修改学生信息\n4.查询学生列表信息\n5.查询单一学生信息\n6.退出系统");
        int choice = SysInput.readInt("请选择:");
        switch (choice) {
            case 1:
                addStuView();
                break;
            case 2:
                deleteStuView();
                break;
            case 3:
                updateStuView();
                break;
            case 4:
                queryStuListView();
                break;
            case 5:
                queryStuView();
                break;
            case 6:
                exit();
            default:
                SysOutput.show("输入错误，请重新输入!");
                break;
        }
    }

    public void addStuView() {
        SysOutput.show("请按照提示信息要求输入学生信息！");
        Student student = new Student();
        student.setStuNo(SysInput.readString("请输入学生学号:"));
        student.setName(SysInput.readString("请输入学生姓名:"));
        student.setAge(SysInput.readString("请输入学生年龄:"));
        student.setGender(SysInput.readString("请输入学生性别:"));
        student.setIdCardNumber(SysInput.readString("请输入学生身份证号:"));
        student.setTelephone(SysInput.readString("请输入学生电话:"));
        student.setAddress(SysInput.readString("请输入学生家庭住址:"));
        student.setPoliticalStatus(SysInput.readString("请输入学生政治面貌:"));
        student.setGrade(SysInput.readString("请输入学生年级:"));
        student.setMajor(SysInput.readString("请输入学生专业:"));
        student.setClassNo(SysInput.readString("请输入学生班级:"));
        student.setPoor(SysInput.readString("请输入学生贫困生状态(是/否):"));
        student.setFinancialAidStatus(SysInput.readString("请输入学生助学金状态(是/否):"));

        Family family = new Family();
        family.setRelationship(SysInput.readString("请输入家庭成员与学生关系:"));
        family.setName(SysInput.readString("请输入家庭成员姓名:"));
        family.setTelephone(SysInput.readString("请输入家庭成员电话:"));
        student.setFamily(family);
        stuInfoMgrController.addStu(student);
        SysOutput.show("学生信息添加成功!");
        isExit(1);
    }

    public void deleteStuView() {
        String stuNo = SysInput.readString("请输入要删除学生的学号:");
        if ("y".equalsIgnoreCase(SysInput.readString("确定删除学号[" + stuNo + "]的学生信息？(Y/N)："))) {
            stuInfoMgrController.deleteStu(stuNo);
            SysOutput.show("学号[" + stuNo + "]的学生信息删除成功!");
        }
        isExit(2);
    }

    public void updateStuView() {
        SysOutput.show("请按照提示信息：先查询学生信息再输入修改数据！");
        String stuNo = SysInput.readString("请输入要修改学生的学号:");
        Student student = stuInfoMgrController.queryStu(stuNo);
        if (student == null) {
            SysOutput.show("学号[" + stuNo + "]的学生信息不存在！");
            return;
        }
        SysOutput.show(student.toString());
        SysOutput.show("1.修改学生信息\n2.退出修改");
        int choice = SysInput.readInt("请输入编号:");
        String str = "";
        if (choice == 1) {

            if ("y".equalsIgnoreCase(SysInput.readString("修改学生姓名吗？（Y/N）:"))) {
                student.setName(SysInput.readString("请输入学生姓名:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生年龄吗？（Y/N）:"))) {
                student.setAge(SysInput.readString("请输入学生年龄:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生性别吗？（Y/N）:"))) {
                student.setGender(SysInput.readString("请输入学生性别:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生身份证号吗？（Y/N）:"))) {
                student.setIdCardNumber(SysInput.readString("请输入学生身份证号:"));
            }

            if ("y".equalsIgnoreCase(SysInput.readString("修改学生电话吗？（Y/N）:"))) {
                student.setTelephone(SysInput.readString("请输入学生电话:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生家庭住址吗？（Y/N）:"))) {
                student.setAddress(SysInput.readString("请输入学生家庭住址:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生政治面貌吗？（Y/N）:"))) {
                student.setPoliticalStatus(SysInput.readString("请输入学生政治面貌:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生年级吗？（Y/N）:"))) {
                student.setGrade(SysInput.readString("请输入学生年级:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生专业吗？（Y/N）:"))) {
                student.setMajor(SysInput.readString("请输入学生专业:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生班级吗？（Y/N）:"))) {
                student.setClassNo(SysInput.readString("请输入学生班级:"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生贫困生状态吗？（Y/N）:"))) {
                student.setPoor(SysInput.readString("请输入学生贫困生状态(是/否):"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生助学金状态吗？（Y/N）:"))) {
                student.setFinancialAidStatus(SysInput.readString("请输入学生助学金状态(是/否):"));
            }
            if ("y".equalsIgnoreCase(SysInput.readString("修改学生家庭成员信息吗？（Y/N）:"))) {
                Family family = new Family();
                family.setRelationship(SysInput.readString("请输入家庭成员与学生关系:"));
                family.setName(SysInput.readString("请输入家庭成员姓名:"));
                family.setTelephone(SysInput.readString("请输入家庭成员电话:"));
                student.setFamily(family);
            }
            stuInfoMgrController.updateStu(student);
            SysOutput.show("学生信息修改成功!");
        } else if (choice == 2) {
            isExit(3);
        } else {
            SysOutput.show("输入错误，请重新输入!");
        }
        isExit(3);
    }

    public void queryStuListView() {
        SysOutput.show("学生列表信息如下");
        List<Student> students = stuInfoMgrController.queryStuList();
        for (Student student:students) {
            if (student != null) {
                System.out.println(student.toString());
            }
        }
        isExit(4);
    }

    public void queryStuView() {
        String stuNo = SysInput.readString("请输入要查询学生的学号:");
        Student student = stuInfoMgrController.queryStu(stuNo);
        if (student == null) {
            SysOutput.show("学号[" + stuNo + "]的学生信息不存在！");
            return;
        } else {
            SysOutput.show(student.toString());
        }
        isExit(5);
    }

    // 是否退出
    public void isExit(int option) {
        SysOutput.show("1.继续当前操作\n2.退到主菜单\n3.退出系统");
        int e = SysInput.readInt("请选择:");
        if (e == 1) {
            if (option == 1) {
                addStuView();
            }
            if (option == 2) {
                deleteStuView();
            }
            if (option == 3) {
                updateStuView();
            }
            if (option == 4) {
                queryStuListView();
            }
            if (option == 5) {
                queryStuView();
            }
        } else if (e == 2) {
            start();
        } else {
            exit();
        }
    }

    // 退出系统
    public void exit() {
        SysOutput.show("谢谢使用！再见~");
        System.exit(0);
    }
}
